package icloud.com.myapplication.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import icloud.com.myapplication.R;

public class WebViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        final WebView  webView= (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setDomStorageEnabled(true);
        // 设置可以访问文件
        webSettings.setAllowFileAccess(true);
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        // 设置默认缩放方式尺寸是far
        // 设置出现缩放工具
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setLoadWithOverviewMode(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

        }
//        webView.loadUrl("https://m-test.5i5j.com/appcms/wiki?city_id=8&category3=735");
//        webView.loadUrl("http://m.5i5j.com.cn/zt/event20180302/index-test.html");
        webView.loadUrl("file:///android_asset/test.html");
        webView.addJavascriptInterface(new JsCallAndroid(), "android");
        webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }

            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onPageFinished(WebView view, String url) {
//                view.loadUrl("javascript:(function launchSecondList(){window.android" +
//                        ".launchSecondList();})()");
                webView.evaluateJavascript("jsCallBack()", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Toast.makeText(WebViewActivity.this,"Android调用jsCallBack方法",Toast.LENGTH_LONG).show();
                    }
                });
                super.onPageFinished(view, url);
            }
        });
        
    }

    class JsCallAndroid {
        @JavascriptInterface
        public void launchSecondList() {
            Toast.makeText(WebViewActivity.this,"Js调用Android方法",Toast.LENGTH_LONG).show();
            startActivity(new Intent(WebViewActivity.this,MainActivity.class));
        }

        @JavascriptInterface
        public void test() {
            Toast.makeText(WebViewActivity.this,"test",Toast.LENGTH_SHORT).show();
        }
        
    }

    public static class ToolBarActiivty extends AppCompatActivity {
    
        private  Bitmap mBitmap;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tool_bar_actiivty);
        }
    }
}
